package org.dtrd.modules.doctor.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.config.system.Constant;
import org.dtrd.config.system.Constant.DoctorConcernPatientStatus;
import org.dtrd.modules.doctor.entity.bean.DoctorStatisticsItem;
import org.dtrd.modules.doctor.entity.po.DtrdEntRdDoctorConcernPatient;
import org.dtrd.modules.doctor.mapper.DtrdEntRdDoctorConcernPatientMapper;
import org.dtrd.modules.doctor.service.IDtrdEntRdDoctorConcernPatientService;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Set;


/**
 * <p>
 * 患者重点关注记录表 服务实现类
 * </p>
 *
 * @author xiao
 * @since 2022-06-17
 */
@DS("multi-datasource1")
@Service
@Slf4j
public class DtrdEntRdDoctorConcernPatientServiceImpl extends ServiceImpl<DtrdEntRdDoctorConcernPatientMapper, DtrdEntRdDoctorConcernPatient> implements IDtrdEntRdDoctorConcernPatientService {

    /**
     * @param doctorId
     * @param patientId
     * @param patientsCollected 1 取关 2 关注
     * @param orgCode
     * @return
     */
    @Override
    public boolean concernPatient(Integer doctorId, Integer patientId, Integer patientsCollected, String orgCode) {
        DtrdEntRdDoctorConcernPatient po = baseMapper.getConcernRecord(patientId, doctorId);
        log.info("DtrdEntRdDoctorConcernPatientServiceImpl.concernPatient.[doctorId={}, patientId={}, patientsCollected={}, orgCode={},po={}", doctorId, patientId, patientsCollected, orgCode, po);
        if (patientsCollected == DoctorConcernPatientStatus.CONCERN.getStatus()) {
            // 添加关注
            if (po != null) {
                // 已关注 不作处理
                return true;
            } else {
                po = new DtrdEntRdDoctorConcernPatient().init();
                po.setDoctorId(doctorId);
                po.setPatientId(patientId);
                po.setOrgCode(orgCode);
                return save(po);
            }
        } else {
            //取关
            if (po != null) {
                return removeById(po.getDataId());
            } else {
                // 未关注  不作处理
                return true;
            }
        }
    }

    @Override
    public List<DoctorStatisticsItem> getConcernPatientCount(Integer doctorId, String orgCode) {
        return baseMapper.getConcernPatientCount(doctorId);
    }

    @Override
    public List<DtrdEntRdDoctorConcernPatient> getConcernRecords(Integer doctorId) {
        return baseMapper.getConcernRecordList(doctorId, null);
    }

    @Override
    public void deleteRecordByPatientIds(@NotEmpty Set<Integer> patientIds) {
        baseMapper.deleteRecordByPatientIds(patientIds);
    }
}
